<?php

Class Permissions extends Database{

	private $pid;//permmissions id
	private $perms = array();
	private $isShell = false;

		//1=guest
		//2=user
		//3=mod
		//4=admin
	public function __construct($perm_id=0){
		$this->pid = $perm_id;
		
		parent::__construct();

		if($this->pid !== 0){

			//Attempt to load self
			$this->perms = $this->get_row('perms','pid',$this->pid);
			$this->isShell = false;
			if($this->perms !== null) return;
		}
		//attempt to load guest permissions (shell)
		$this->isShell = true;
		$this->perms = $this->get_row('perms','pid',1);
		if($this->perms !== null) return;
		
		//Oh noes? throw excpeton and return null
		
		return null;
	}

	public function write(){
		//Load are details to an array (since this contains all are info)
		$permDataArray = $this->perms;
		//override specal details
		$permDataArray['pid'] = $this->pid;
			
			
		if($this->isShell){
			//id will be ignored so no worrys with setting it
			$this->create_row('perms',$permDataArray);
		}else{

			//update current details
			//Write ourselves and all are stats back to the database!
			$this->set_row('perms','pid',$this->pid,$permDataArray); // now we pray
		}
		return true;
	}

	/*
	get
	get single Permission from users data array
	*/
	public function get($action){
		if(isset($this->perms[$action])){
			return $this->perms[$action]; // if the attribute exists, grab it
		}else{
			return 'no value'; // else return error
		}
	}
	/*
	set
	change an actions status from true to false;
	*/
	public function set($action,$value){
		if(isset($this->perms[$action])){//if attribute exists
			$this->perms[$action] = $value;//else  set value and return true
			return true;
		}else{
			return false;
		}
	}

	/*
	getAttriubuteArray
	return all users attributes as an array
	*/
	public function getAsArray(){
		return $this->perms;
	}

}